package com.financing.platform.web.api;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.platform.common.ResultCode;
import com.financing.platform.common.ResultMap;
import com.financing.platform.common.ResultMap.STATUS;
import com.financing.platform.dto.AccessInfoDTO;
import com.financing.platform.dto.ActionDTO;
import com.financing.platform.dto.GroupDTO;
import com.financing.platform.service.IAccessControlService;
import com.financing.platform.service.IActionService;
import com.financing.platform.service.IGroupService;
import com.financing.platform.util.DTOdateSet;
import com.financing.platform.util.Dict;
import com.financing.platform.util.StringUtil;

/**
 * 
* @ClassName: SearchGroup 
* @Description: 组管理 
* @author chenming 
* @date 2014年7月21日 上午11:45:33 
*
 */
@Controller("GroupActionApi")
@RequestMapping("/api/group")
@Scope("prototype")
public class GroupAction extends BaseAction{
	
	@Autowired
    IGroupService groupServiceImpl;
	
	@Autowired
	IAccessControlService accessControlServiceImpl;
	
	@Autowired
	IActionService actionServiceImpl;
	
	@RequestMapping("/actionList.shtml")
	@ResponseBody
	public ResultMap actionList(HttpServletRequest request,
			HttpServletResponse response ) {
	
		try{
			List<ActionDTO> actionList = actionServiceImpl.queryAllAction();
			resultMap.setAttribute("actionList", actionList);
			
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			
		}catch(Exception e){
			log.error("actionList error !",e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		return resultMap;
	}
	
	/**
	 * 
	* @Title: searchAllGroup 
	* @Description: 查询所有组信息概要
	* @param   request
	* @param   response
	* @return ModelAndView   
	* @throws
	 */
	@RequestMapping("/queryGroup.shtml")
	@ResponseBody
	public ResultMap searchAllGroup(HttpServletRequest request,
			HttpServletResponse response,GroupDTO group) {
		try{
			this.log.info("begin query all Group!");
			
			resultMap.setAttribute("allGroup",groupServiceImpl.queryAllGroup(group));
			
			this.log.info("end query all Group !");
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			
			log.info("End  query all Group!");
		}catch(Exception e){
			log.error("searchAllGroup error !",e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		return resultMap;
	}
	
	/**
	 * 
	* @Title: searchAllGroup 
	* @Description: 查询所有组信息概要
	* @param   request
	* @param   response
	* @return ModelAndView   
	* @throws
	 */
	@RequestMapping("/queryGroupInfo.shtml")
	@ResponseBody
	public ResultMap queryGroupInfo(HttpServletRequest request,
			HttpServletResponse response,GroupDTO group) {
		try{
			this.log.info("begin query one Group!");
			
			resultMap.setAttribute("groupInfo",groupServiceImpl.queryGroup(group));
			
			List<AccessInfoDTO> accessForGroupList = accessControlServiceImpl.queryAccessForGroup(group);
			
			resultMap.setAttribute("accessForGroup", accessForGroupList);
			
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			
			log.info("End query one Group!");
		}catch(Exception e){
			log.error("queryGroupInfo error !",e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return resultMap;
	}
	
	/**
	 * 
	* @Title: insertGroup 
	* @Description: 添加组
	* @param   request
	* @param   response
	* @param   groupDto
	* @return ModelAndView   
	* @throws
	 */
	@RequestMapping("/addGroup.shtml")
	@ResponseBody
	public ResultMap insertGroup(HttpServletRequest request,
			HttpServletResponse response,GroupDTO groupDto) {
		
		this.log.info("begin add  Group!");
		
		try{

			Map<String,Integer> mode = new HashMap<String,Integer>();
			
			if(!StringUtil.isNullOrEmptyStr(groupDto.getGroupName()) && !StringUtil.isNullOrEmptyStr(groupDto.getGroupRemark())){
				DTOdateSet.setCreateInfo(groupDto, request.getSession());
				DTOdateSet.setUpdateInfo(groupDto, request.getSession());
				
				mode.put("result", groupServiceImpl.addGroup(groupDto));
				
				accessControlServiceImpl.deleteAccessForGroup(groupDto);
				
				String[] actionIds = request.getParameter("actionId").split("[,]");
				
				AccessInfoDTO accessInfoDTO = new AccessInfoDTO();
				DTOdateSet.setCreateInfo(accessInfoDTO, request.getSession());
				DTOdateSet.setUpdateInfo(accessInfoDTO, request.getSession());
				accessInfoDTO.setGroupId(groupDto.getGroupId());
				accessInfoDTO.setEnable(Dict.accessEnable);
				accessControlServiceImpl.addAccessForGroup(actionIds,accessInfoDTO);
			}
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			
			log.info("End query one Group!");
		}catch(Exception e){
			log.error("queryGroupInfo error !",e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
			
		
		return resultMap;
	}
	
	/**
	 * 
	* @Title: queryGroupAccess 
	* @Description: 更新组权限信息
	* @param   request
	* @param   response
	* @param   groupDto
	* @return ModelAndView   
	* @throws
	 */
	@RequestMapping("/updateGroupInfo.shtml")
	@ResponseBody
	public ResultMap updateGroupInfo(HttpServletRequest request,
			HttpServletResponse response,GroupDTO groupDto) {
		try{
			this.log.info("begin update group info!");
			
			groupServiceImpl.updateGroup(groupDto);
			
			this.log.info("end update group info!");
			
			String[] actionIds = request.getParameter("actionId").split("[,]");
			
			this.log.info("begin update group access !");
			AccessInfoDTO accessInfoDTO = new AccessInfoDTO();
			DTOdateSet.setCreateInfo(accessInfoDTO, request.getSession());
			DTOdateSet.setUpdateInfo(accessInfoDTO, request.getSession());
			accessInfoDTO.setGroupId(groupDto.getGroupId());
			
			accessControlServiceImpl.deleteAccessForGroup(groupDto);
			accessInfoDTO.setEnable(Dict.accessEnable);
			accessControlServiceImpl.addAccessForGroup(actionIds,accessInfoDTO);
			
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			
			this.log.info("end update group access !");
		}catch(Exception e){
			log.error("updateGroupInfo error !",e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return resultMap;
	}
	
}
